package cn.bdqn.fragment_went.activity;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.bdqn.fragment_went.R;
import cn.bdqn.fragment_went.adapter.MyViewAdapter;
import cn.bdqn.fragment_went.fragemnt.ConcatFragment;
import cn.bdqn.fragment_went.fragemnt.FindFragment;
import cn.bdqn.fragment_went.fragemnt.MineFragment;
import cn.bdqn.fragment_went.fragemnt.WechatFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager vpMain;
    private LinearLayout llWechat, llFind,llConcat, llMine;
    private ImageView ivWechat,ivFind, ivConcat, ivMine;
    private List<Fragment> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initDate();
        MyViewAdapter adapter=new MyViewAdapter(getSupportFragmentManager(),list);
        vpMain.setAdapter(adapter);

        vpMain.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            //i的值为2的时候,说明进行滑动了
            @Override
            public void onPageScrollStateChanged(int i) {
                if (i==2) {
                    //Toast.makeText(MainActivity.this, vpMain.getCurrentItem(), Toast.LENGTH_SHORT).show();
                     setItem(vpMain.getCurrentItem());
                }
            }
        });
    }

    private void initView() {
        vpMain=findViewById(R.id.vp_main);

        llWechat=findViewById(R.id.ll_wechat);
        llFind=findViewById(R.id.ll_find);
        llConcat=findViewById(R.id.ll_concat);
        llMine=findViewById(R.id.ll_mine);

        ivWechat=findViewById(R.id.iv_wechat);
        ivFind=findViewById(R.id.iv_find);
        ivConcat=findViewById(R.id.iv_concat);
        ivMine=findViewById(R.id.iv_mine);

        llWechat.setOnClickListener(this);
        llFind.setOnClickListener(this);
        llConcat.setOnClickListener(this);
        llMine.setOnClickListener(this);

    }

    private void initDate() {
    list=new ArrayList<Fragment>();
        WechatFragment wechatFragment=new WechatFragment();
        ConcatFragment concatFragment=new ConcatFragment();
        FindFragment findFragment=new FindFragment();
        MineFragment mineFragment=new MineFragment();
        list.add(wechatFragment);
        list.add(concatFragment);
        list.add(findFragment);
        list.add(mineFragment);


    }
    @Override
    public void onClick(View view){
        setItem(view.getId());
    }
    public void setItem(int i){
        switch (i){
            case R.id.ll_wechat:
            case  0:
                //第一种方式设置动态图片
                //ivWechat.setImageDrawable(getResources().getDrawable(R.drawable.wechat2));
                //第二种
                //ivWechat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.wechat2));
                ivWechat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.wechat));
                setImageGray();
                vpMain.setCurrentItem(0);
                break;
            case R.id.ll_concat:
            case  1:
                ivWechat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.concat));
                setImageGray();
                vpMain.setCurrentItem(1);
                break;
            case R.id.ll_find:
            case  2:
                ivWechat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.find));
                setImageGray();
                vpMain.setCurrentItem(2);
                break;
            case R.id.ll_mine:
            case  3:
                ivWechat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.mine));
                setImageGray();
                vpMain.setCurrentItem(3);
                break;
            default:break;

        }
    }
    public  void setImageGray(){
        ivWechat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.wechat2));
        ivConcat.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.concat2));
        ivFind.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.find2));
        ivMine.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.mine2));

    }

}
